/*
 * app交互处理
 * @Author: kanglin
 * @Date: 2018-12-24 11:04:40
 * @Copyright: Created by Panxsoft.
 */
import { $t } from 'src/i18n';

/**
 *  方法: productSearch 首页导航查询的方法名
 *  @see https://panxsoft.coding.net/p/Direwolf-Web/task/172941
 */

export const appHandle = {
	name: 'appHandle',
	methods: {
		/**
		 * app的交互处理
		 * @param  {String} fn       {交互的方法名}
		 * @param  {Any} param {参数}
		 */
		handleApp(fn, param) {
			let req_param;
			if (typeof param === 'object') {
				req_param = JSON.stringify(param);
			} else {
				req_param = param;
			}

			try { // for iOS
				window.webkit.messageHandlers[fn].postMessage(req_param);
				/* eslint-disable no-console */
				console.log('ios:', fn, param);
			} catch (e) { // for Android
				/* global DIREWOLF */ // for Android
				try {
					if (req_param) {
						DIREWOLF[fn](req_param);
					} else {
						DIREWOLF[fn]();
					}
				} catch (error) {
					console.log(error.message, fn, param);
					/* eslint-disable no-alert */
					alert($t('common.app_error'));
				}
			}
		},

		
	},
};
